#include <stdarg.h>
#include <string>

#include "GAssert.h"
#include <Support\Log.h>
#include <Game.h>

GAssert GAssert::theInstance;

GAssert& GAssert::getInstance()
{
	return theInstance;
}

void GAssert::HandleSkippableAssert( const char* file_name, const char* function_name, int line_number, const char* expr, const char* fmt, ... )
{
	char buffer0[1024];
	char buffer1[1024];
	va_list args;
	va_start( args, fmt );
	vsprintf_s(buffer0, sizeof(buffer0), fmt, args);
	vsprintf_s(buffer0, sizeof(buffer0), fmt, args);
	std::string assertMsg;
	sprintf_s(buffer1, sizeof(buffer1), 
		"*** Assertion failed in \n%s,\n line %d,\n function %s\n*** %s\n***: %s\n", file_name, line_number, function_name, expr, fmt);
	va_end( args );
	assertMsg.clear();
	assertMsg = buffer1;
	assertMsg += buffer0;
	gLog(assertMsg.c_str());
	if (NULL != game)
		game->terminate();
}

	bool GAssert::initialize(bool quiet)
	{
		static const char* welcomeMessage = "Assert Manager Started...";
		quietMode = quiet;
		if (! quietMode)
			gLog(welcomeMessage);
		return true;
	}

	bool GAssert::initialize(Game* ptrGame, bool quiet)
	{
		game = ptrGame;
		return initialize(quiet);
	}
	bool GAssert::shutdown()
	{
		static const char* finalMessage = "Assert Manager Shutting down...";
		if (! quietMode)
			gLog(finalMessage);
		return true;
	}

